package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"plan-sys/cache"
	"plan-sys/model"
	"plan-sys/pkg"
	"strconv"
	"time"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	var user model.User
	err := model.DB.Where("username=?", username).First(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码不正确",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号不存在",
		})
		return
	}
	if user.Password != pkg.Md5Str(password) {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码不正确",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登入成功",
		"data": user,
	})
	return
}
func User(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))
	data := model.RoleNode{
		Rid: rid,
		Nid: nid,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func Rold(c *gin.Context) {
	uid := c.Query("uid")
	var role []model.Role
	//SELECT
	//*
	//	FROM
	//roles
	//LEFT JOIN user_roles ON roles.id=user_roles.rid
	//WHERE user_roles.uid=1
	model.DB.Table("roles").
		Joins("LEFT JOIN user_roles ON roles.id=user_roles.rid").
		Where("user_roles.uid=?", uid).
		Debug().First(&role)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": role,
	})
	return
}
func Node(c *gin.Context) {
	rid := c.Query("rid")
	var node []model.Node
	/*
	   SELECT
	   *
	   FROM
	   nodes
	   LEFT JOIN role_nodes ON nodes.id=role_nodes.nid
	   WHERE role_nodes.rid=1
	*/
	model.DB.Table("nodes").
		Joins("LEFT JOIN role_nodes ON nodes.id=role_nodes.nid").
		Where("role_nodes.rid=?", rid).Debug().Find(&node)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": node,
	})
	return
}
func UserRole(c *gin.Context) {
	name := c.PostForm("name")
	price := c.PostForm("price")
	lei := c.PostForm("lei")
	li := c.PostForm("li")
	if name == "" || price == "" || lei == "" || li == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := model.Che{
		Name:  name,
		Lei:   lei,
		Price: price,
		Li:    li,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func Add(c *gin.Context) {
	ke := c.PostForm("ke")
	che := c.PostForm("che")
	prices := c.PostForm("prices")
	cid, _ := strconv.Atoi(c.PostForm("cid"))
	if ke == "" || che == "" || prices == "" || cid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := model.Ding{
		Ke:     ke,
		Che:    che,
		Prices: prices,
		Cid:    cid,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	var user []model.CheDing
	redis_key := "list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	/*
		SELECT
		dings.id AS DingId,
		ches.name AS Name,
		ches.lei AS Lei,
		ches.price AS Price,
		ches.li AS Li,
		dings.ke AS Ke,
		dings.che AS Che,
		dings.prices AS Prices
		FROM
		dings
		LEFT JOIN ches ON ches.id=dings.cid
	*/
	if cacheData == "" {
		model.DB.Table("dings").
			Select(
				"dings.id AS DingId",
				"ches.name AS Name",
				"ches.lei AS Lei",
				"ches.price AS Price",
				"ches.li AS Li",
				"dings.ke AS Ke",
				"dings.che AS Che",
				"	dings.prices AS Prices",
			).
			Joins("LEFT JOIN ches ON ches.id=dings.cid").Offset(offset).Limit(size).Find(&user)
		jsonData, _ := json.Marshal(&user)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &user)
	}
	var count int64
	model.DB.Model(&model.Ding{}).Count(&count)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  user,
		"count": count,
	})
	return
}
func Detail(c *gin.Context) {
	id := c.Query("id")
	var user []model.CheDing
	redis_key := "detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	/*
		SELECT
		dings.id AS DingId,
		ches.name AS Name,
		ches.lei AS Lei,
		ches.price AS Price,
		ches.li AS Li,
		dings.ke AS Ke,
		dings.che AS Che,
		dings.prices AS Prices
		FROM
		dings
		LEFT JOIN ches ON ches.id=dings.cid
	*/
	if cacheData == "" {
		model.DB.Table("dings").
			Select(
				"dings.id AS DingId",
				"ches.`name` AS Name",
				"ches.lei AS Lei",
				"ches.price AS Price",
				"ches.li AS Li",
				"dings.ke AS Ke",
				"dings.che AS Che",
				"dings.prices AS Prices",
			).
			Joins("LEFT JOIN ches ON ches.id=dings.cid").Where("dings.id=?", id).Debug().Find(&user)
		jsonData, _ := json.Marshal(&user)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &user)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": user,
	})
	return
}
